﻿using NPortableEngine;
using Services4KouDai.Encrypt;
using Services4KouDai.Models;
using Services4KouDai.Parsers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Services4KouDai.Services
{
    public class CommonService
    {
        /// <summary>
        /// 统一请求的封装
        /// </summary>
        /// <param name="url">请求的地址</param>
        /// <param name="bodyParas">请求的参数</param>
        ///  /// <param name="data">数据</param>
        /// <param name="parser">解析器</param>
        /// <returns>统一返回参数</returns>
        public static async Task<ServiceRespInfo> ServiceReq(string url, Dictionary<string, object> bodyParas, object data, IParser parser)
        {
            Dictionary<string, object> paras = new Dictionary<string, object>();

            paras.Add("header", ServiceConstants.CommonHeaders);
            if (bodyParas != null)
            {
                paras.Add("body", bodyParas);
            }

            string edata = paras.EncryptDic();

            return (await new HttpQueryer().RequestAsync(url, HttpMethodType.POST, ReqType.JSON)
                .Header("encryType", "3")
                .Header("gzipType", "0")
                .Header("Accept-Encoding", "geili-zip")
                .Header("Content-Encoding", "geili-zip")
                .Params("edata", edata)
                .Decompress(CommonDecompressor.GetDecompresssFunc())
                .Parse(parser)
                .FinalNeedData(data)
                .Fly().ConfigureAwait(false)).ToServiceInfo();
        }
    }
}
